package tech.abing.spider.task.pageprocessor.csrc;

import java.util.List;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.TaskConstants;
import tech.abing.spider.task.model.csrc.CommissionDataEntity;
import tech.abing.spider.task.utils.TaskUtil;

public class CommissionDataProcessor implements PageProcessor {

	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context, List<BasicRequest> queue,
			List<Proccessable> objectContainer) throws Exception {

		Document doc = Jsoup.parse(page.getContent());
		
		String url = page.getRequest().getUrl();
		String pageName = url.substring(url.lastIndexOf("/")+1);
		String pageNameDefine = TaskConstants.COMMISSION_INDEX_PAGE + "." + TaskConstants.PAGE_SUBFIX;
		
		String infoType = page.getRequest().getParamsByName("infoType").toString();
		
		if(pageNameDefine.equals(pageName)){
			//计算总页数
			Elements eles = doc.getElementsByTag("SCRIPT");
			int totalRecords = 0, pageSize = 20, totalPages = 0;
			if(eles != null && eles.size() > 0){
				String pageText = eles.get(7).html();
				
				String[] pageTextArr = pageText.split(";");
				
				String totalRecordsStr = pageTextArr[0].substring(pageTextArr[0].indexOf("\"")+1, pageTextArr[0].lastIndexOf("\""));
				totalRecords = TaskUtil.str2Int(totalRecordsStr);
				pageSize = TaskUtil.str2Int(pageTextArr[3].substring(pageTextArr[3].indexOf("=")+1));
			}
			if(pageSize != 0){
				totalPages = totalRecords % pageSize == 0 ? totalRecords / pageSize : totalRecords / pageSize + 1 ;
			}
			
			if(totalRecords == 0){
				totalPages = 1;
			}
			
			for(int i = 2; i <= totalPages; i++){//从第二页开始
				
				url = url.substring(0, url.lastIndexOf("/")) + "/" + (TaskConstants.COMMISSION_INDEX_PAGE+"_"+(i-1)+"."+TaskConstants.PAGE_SUBFIX);
				PageRequest req = context.createPageRequest(url, CommissionDataProcessor.class);
				req.putParams("infoType", infoType);
				
				queue.add(req);
				
			}
		}
		
		//解析数据
		Elements eles = doc.select("li.mc a[onMouseOver*=\"displayTip\"]");
		for(Element ele : eles){
			//../../G00306212/201609/t20160907_303118.htm
			//http://www.csrc.gov.cn/pub/zjhpublic/3300/3313/index_7401_34.htm
			//==>http://www.csrc.gov.cn/pub/zjhpublic/G00306212/201609/t20160907_303118.htm
			String href = ele.attr("href").replace("../", "");
			url = TaskConstants.COMMISSION_MAIN_PAGE + "/" + href;
			
			PageRequest req = context.createPageRequest(url, CommissionContentProcessor.class);
			req.putParams("infoType", infoType);
			
			queue.add(req);
		}
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
